{% extends 'base.html' %}
{% load crm_tags %}

{% block custome_css %}
    <link rel="stylesheet" href="/static/plugin/dropzone/dropzone.min.css">
    <style>
        .dropzone {
            border: 2px dashed #0087F7;
            border-radius: 5px;
            background: white;
        }

        .dropzone .dz-message {
            font-weight: 400;
        }

        .dropzone .dz-message .note {
            font-size: 0.8em;
            font-weight: 200;
            display: block;
            margin-top: 1.4rem;
        }
    </style>
{% endblock %}

{% block body %}
    <div class="container col-lg-8 col-lg-offset-2">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title">报名确认</h3>
            </div>
            <div class="panel-body">
                {% if is_agreed %}
                    您已报名
                {% else %}
                    <form id="registriation" class="form-horizontal" role="form" method="post" onsubmit="return RegisterFormChceck();">
                        {% csrf_token %}
                        {% for field in form_obj %}
                            <div class="form-group">
                                <label for="" class="col-sm-2 control-label">
                                    {{ field.label }}
                                </label>
                                <div class="col-md-6 col-sm-6">
                                    {{ field }}<span
                                        style="color: red; margin-right: 5px">{{ field.errors.get_json_data.0.message }}</span>
                                </div>
                            </div>
                        {% endfor %}
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">所报班级</label>
                            <div class="col-md-6 col-sm-6"
                                 style="margin-top: 7px">{{ enrollment_obj.enrolled_class.course.name }}</div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">课程费用</label>
                            <div class="col-md-6 col-sm-6"
                                 style="margin-top: 7px">{{ enrollment_obj.enrolled_class.course.price }}元
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="" class="col-sm-2 control-label">开课日期</label>
                            <div class="col-md-6 col-sm-6"
                                 style="margin-top: 7px">{{ enrollment_obj.enrolled_class.start_date|date:"Y-m-d" }}</div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-12 col-sm-12">
                                <pre>{% render_contract enrollment_obj %}</pre>
                            </div>
                            <div class="col-md-12 col-sm-12">
                                <input type="checkbox"/>我已认证阅读完协议并接受所有条款
                            </div>
                        </div>
                    </form>
                    <hr/>
                    已上传文件:
                    <ul id="file_list"></ul>
                    <hr/>

                    <form id="fileDropzone" action="{{ request.path_info }}" class="dropzone" method="post">
                        {% csrf_token %}
                        <div class="dz-message">拖拽文件到此处</div>
                    </form>
                    <hr/>
                    <button id="submit" type="submit" class="pull-right btn btn-success">提交</button>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block custome_js %}
    <script src="/static/plugin/dropzone/dropzone.min.js"></script>
    <script>
        $(function () {
            $('#submit').click(function () {
                $('#registriation').submit();
            });

            Dropzone.autoDiscover = false;
            $("#fileDropzone").dropzone({
                paramName: "file", // The name that will be used to transfer the file
                maxFilesize: 2, // MB
                maxFiles: 2,
                uploadMultiple: true,    //一次性上传多个文件
                acceptedFiles: '.jpg,.png',
                addRemoveLinks: true,
                dictInvalidFileType: '只接收图片',
                dictRemoveFile: '点击删除该文件',
                init: function () {
                    myDropzone = this;
                    this.on('removedfile',function (file) {
                        $.get("/crm/file_delete/{{ enrollment_obj.id }}/"+file.name,function (data) {
                            getFileList();
                        })
                    })
                },
                accept: function(file, done) {
                    done();
                },
                success: function (file,data) {
                    {#getFileList()#}
                }
            });
            $('#file_list').delegate('.delete_file','click',function () {
                $.ajaxSetup({async : false});
                $.get($(this).attr('href'), function(data){});
                $.ajaxSetup({async : true});
                $(this).parent().remove();
                return false;
            });
            getFileList();
        });

        function RegisterFormChceck() {
            if ($('form :checkbox').prop('checked')) {
                if(getFileListLength() <2 ){
                    alert('请先上传身份证照片');
                    return false;
                }
                $('form').find('[disabled="disabled"]').removeAttr('disabled');
                return true;
            } else {
                alert('必须同意条款');
                return false;
            }

        }
        // 获取已上传文件
        function getFileList() {
            $.get("/crm/file_list/{{ enrollment_obj.id }}/", function(data){
                $('#file_list').empty();
                var file_list = $.parseJSON(data);
                $.each(file_list,function(index,value){
                    var li_ele = document.createElement('li');
                    $(li_ele).html(value+'<a class="delete_file" href="/crm/file_delete/{{ enrollment_obj.id }}/'+value+'">&lt; 点击删除</a>');
                    $('#file_list').append(li_ele);
                });
            });
        }
        // 获取已上传文件数量
        function getFileListLength() {
            var file_list = [];
            $.ajaxSetup({async : false});
            $.get("/crm/file_list/{{ enrollment_obj.id }}/", function(data) {
                file_list =$.parseJSON(data);
            });
            $.ajaxSetup({async : true});
            return file_list.length;
        }

    </script>
{% endblock %}